#!/bin/sh
#
# chkconfig: 2345 88 61
# description: UYUN APM.

. /etc/rc.d/init.d/functions

exec="/opt/apm/analyze/dist/script/run_apm.sh"
prog_apm="apm"
lockfile_apm="/var/lock/apm/$prog_apm.lock"
prog_dssl="apmdssl"
lockfile_dssl="/var/lock/apm/$prog_dssl.lock"

mkdir -p /var/lock/apm

echo_running() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $"  running  "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\n"
  return 0
}

echo_stopped() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo -n $"  stopped  "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\n"
  return 1
}

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $prog_apm: "
    sh $exec
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile_apm
    return $retval
}

stop() {
    echo -n $"Stopping $prog_dssl: "
    killproc $prog_dssl
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile_dssl

    echo -n $"Stopping $prog_apm: "
    killproc $prog_apm
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile_apm
    
    return $retval
}

restart() {
    stop
    start
}

rh_status() {
    pid=$(pidof $prog_apm)
    if [ -z $pid ]; then
        echo -n "UYUN APM Analyzer apm" && echo_stopped
    else
        echo -n "UYUN APM Analyzer apm(pid $pid)" && echo_running
    fi
    
    pid=$(pidof $prog_dssl)
    if [ -z $pid ]; then
        echo -n "UYUN APM Analyzer dssl" && echo_stopped
    else
        echo -n "UYUN APM Analyzer dssl(pid $pid)" && echo_running
    fi
    
    return 1
}

case "$1" in
    start)
        $1
        ;;
    stop)
        $1
        ;;
    restart)
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
esac
exit $?
